package com.chase.passwordmaster.add

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
import com.chase.passwordmaster.R
import androidx.lifecycle.ViewModelProviders
import com.chase.passwordmaster.data.AccountViewModel
import com.chase.passwordmaster.entity.DecryptedAccount
import com.chase.passwordmaster.utils.showAlert
import com.chase.passwordmaster.utils.showToast
import com.chase.passwordmaster.utils.useDarkStatusBar
import com.chase.passwordmaster.utils.useImmersingNavigator
import com.mylhyl.circledialog.CircleDialog

class AddActivity : AppCompatActivity() {

    private lateinit var accountViewModel: AccountViewModel

    private lateinit var cpBack: View
    private lateinit var btSave: View
    private lateinit var cpWebsite: EditText
    private lateinit var cpUsername: EditText
    private lateinit var cpPassword: EditText
    private lateinit var cpName: EditText
    private lateinit var cpTel: EditText
    private lateinit var cpQQ: EditText
    private lateinit var cpWeChat: EditText
    private lateinit var cpEmail: EditText
    private lateinit var cpMemo: EditText

    companion object {
        fun actionStart(context: Context) {
            val intent = Intent(context, AddActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add)

        accountViewModel = ViewModelProviders.of(this).get(AccountViewModel::class.java)

        cpBack = findViewById(R.id.back_add)
        btSave = findViewById(R.id.save_add)
        cpWebsite = findViewById(R.id.et_website_add)
        cpUsername = findViewById(R.id.et_username_add)
        cpPassword = findViewById(R.id.et_password_add)
        cpName = findViewById(R.id.et_name_add)
        cpTel = findViewById(R.id.et_tel_add)
        cpQQ = findViewById(R.id.et_qq_add)
        cpWeChat = findViewById(R.id.et_weChat_add)
        cpEmail = findViewById(R.id.et_email_add)
        cpMemo = findViewById(R.id.et_memo_add)

        useDarkStatusBar()
        useImmersingNavigator()

        initCPBack(cpBack = cpBack)
        initSave()
    }

    private fun initCPBack(cpBack: View) {
        cpBack.setOnClickListener { onBackPressed() }
    }

    private fun isAllBlank(): Boolean {
        return cpWebsite.text.isBlank() &&
                cpUsername.text.isBlank() &&
                cpPassword.text.isBlank() &&
                cpTel.text.isBlank() &&
                cpQQ.text.isBlank() &&
                cpWeChat.text.isBlank() &&
                cpEmail.text.isBlank() &&
                cpName.text.isBlank() &&
                cpMemo.text.isBlank()
    }

    override fun onBackPressed() {
        if (!isAllBlank()) {
            // 返回提示
            CircleDialog.Builder()
                .setRadius(20)
                .setWidth(0.8f)
                .setTitle("退出新增账户")
                .setText("\n返回将不会保存填写的信息\n")
                .setPositive("不保存") {
                    super.onBackPressed()
                }
                .setNegative("取消", null)
                .show(supportFragmentManager)
        } else { super.onBackPressed() }
    }

    private fun initSave() {
        fun isRequiredValid() =
            cpWebsite.text.isNotBlank() && cpUsername.text.isNotBlank() && cpPassword.text.isNotBlank()

        btSave.setOnClickListener {
            if (isRequiredValid()) {
                val website = cpWebsite.text.toString()
                val username = cpUsername.text.toString()
                val password = cpPassword.text.toString()
                val name = if (cpName.text.isNotBlank()) cpName.text.toString() else null
                val tel = if (cpTel.text.isNotBlank()) cpTel.text.toString() else null
                val qq = if (cpQQ.text.isNotBlank()) cpQQ.text.toString() else null
                val weChat = if (cpWeChat.text.isNotBlank()) cpWeChat.text.toString() else null
                val email = if (cpEmail.text.isNotBlank()) cpEmail.text.toString() else null
                val memo = if (cpMemo.text.isNotBlank()) cpMemo.text.toString() else null

                val newAccount = DecryptedAccount(
                    website = website,
                    username = username,
                    password = password,
                    name = name,
                    tel = tel,
                    qq = qq,
                    weChat = weChat,
                    email = email,
                    memo = memo
                )
                accountViewModel.addAccount(newAccount)
                this.finish()
            } else {
                showToast("必填项不能为空", Toast.LENGTH_LONG)
            }
        }
    }
}
